<!-- Copyright (c) 2022 渝州大数据实验室
 *
 * Lanius is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *
 *     http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 -->
<!-- 
* @desc:图标
* @fileName:LaniusIcon.vue
* @author:zoujunjie@yzbdl.ac.cn
* @date:2022-05-30
* 说明：基于el-icon封装，实现了颜色、边框等部分的扩展
!-->
<template>
  <div class="lanius-icon" :style="divStyle">
    <el-icon :size="size" :color="displayColor">
      <slot></slot>
    </el-icon>
  </div>
</template>

<script lang="ts" setup>
import { computed, ComputedRef, toRefs } from "vue";
import { getColorByKey } from "@/services/common/style";

interface Props {
  color?: string; //颜色，支持primary、warning等特殊名称和#开头、red、blue等css颜色
  backgroundColor?: string; //背景颜色，支持primary、warning等特殊名称和#开头、red、blue等css颜色
  hasBorder?: boolean; //是否有边框
  isCircleBorder?: boolean; //是否是原形边框
  borderSize?: number; //边框宽度
  size?: number;
  width?: number;
}

const props = withDefaults(defineProps<Props>(), {
  color: "default",
  hasBorder: false,
  isCircleBorder: false,
  size: 14,
  width: 14,
  borderSize: 1,
});

let displayColor: ComputedRef<string> = computed(() => {
  return getColorByKey(props.color);
});

let divStyle = computed(() => {
  let style: any = {
    width: Math.max(props.width, props.size) + "px",
    height: Math.max(props.width, props.size) + "px",
    color: displayColor.value,
  };
  if (props.hasBorder) {
    style.border = `${props.borderSize}px solid ${displayColor.value}`;
  }
  if (props.isCircleBorder) {
    style.borderRadius = "50%";
  }
  if (props.backgroundColor) {
    style.backgroundColor = getColorByKey(props.backgroundColor);
  }
  return style;
});
const { size } = toRefs(props);
</script>

<style lang="scss" scoped>
.lanius-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
</style>
